setwd("~/Desktop/Compare ASVs_16S_New&Old/16S indicator subsets (all ASVs)")
rm(list=ls())
# List of file paths
files <- list(
"Inoculated_With_P" = "16S-Subset-Group Inoculated withP.csv",
"Inoculated_With_MS" = "16S-Subset-Group Inoculated With MS.csv",
"Uninoculated_With_P" = "16S-Subset-Group Uninoculated withP.csv",
"Uninoculated_With_MS" = "16S-Subset-Group Uninoculated With MS.csv",
"Inoculated_Without_P" = "16S-Subset-Group Inoculated withoutP.csv",
"Inoculated_Without_MS" = "16S-Subset-Group Inoculated Without MS.csv",
"Uninoculated_Without_P" = "16S-Subset-Group Uninoculated withoutP.csv",
"Uninoculated_Without_MS" = "16S-Subset-Group Uninoculated Without MS.csv"
)
# List of file paths
files <- list(
"Inoculated_With_P" = "16S-Subset-Group Inoculated withP.csv",
"Inoculated_With_MS" = "16S-Subset-Group Inoculated With MS.csv",
"Uninoculated_With_P" = "16S-Subset-Group Uninoculated withP.csv",
"Uninoculated_With_MS" = "16S-Subset-Group Uninoculated With MS.csv",
"Inoculated_Without_P" = "16S-Subset-Group Inoculated withoutP.csv",
"Inoculated_Without_MS" = "16S-Subset-Group Inoculated Without MS.csv",
"Uninoculated_Without_P" = "16S-Subset-Group Uninoculated withoutP.csv",
"Uninoculated_Without_MS" = "16S-Subset-Group Uninoculated Without MS.csv"
)
# Load ASV IDs from each file
asv_groups <- lapply(files, function(file) {
data <- read.csv(file, header = TRUE, stringsAsFactors = FALSE)
return(data)  # Return the full data (ASV + Taxonomy)
})
# Define comparisons
comparisons <- list(
c("Inoculated_With_P", "Inoculated_With_MS"),
c("Uninoculated_With_P", "Uninoculated_With_MS"),
c("Inoculated_Without_P", "Inoculated_Without_MS"),
c("Uninoculated_Without_P", "Uninoculated_Without_MS")
)
# Loop through comparisons (MODIFIED)
for (comparison in comparisons) {
group1 <- comparison[1]
group2 <- comparison[2]
# Extract full data for both groups
data_g1 <- asv_groups[[group1]]
data_g2 <- asv_groups[[group2]]
# Extract ASV IDs for comparison
asv_g1 <- data_g1[, 1]  # First column contains ASV IDs
asv_g2 <- data_g2[, 1]  # First column contains ASV IDs
# Shared and unique ASVs
shared_asvs <- intersect(asv_g1, asv_g2)
unique_to_g1_asvs <- setdiff(asv_g1, asv_g2)
unique_to_g2_asvs <- setdiff(asv_g2, asv_g1)
# Match taxonomy for shared and unique ASVs (NEW)
shared <- merge(data_g1, data_g2, by = "ASV")
shared <- shared[shared$ASV %in% shared_asvs, , drop = FALSE]  # Shared ASVs with taxonomy
unique_to_g1 <- data_g1[data_g1$ASV %in% unique_to_g1_asvs, , drop = FALSE]  # Unique to group1
unique_to_g2 <- data_g2[data_g2$ASV %in% unique_to_g2_asvs, , drop = FALSE]  # Unique to group2
# Output results
cat("\nComparison:", group1, "vs", group2, "\n")
cat("Shared ASVs:", nrow(shared), "\n")
cat("Unique to", group1, ":", nrow(unique_to_g1), "\n")
cat("Unique to", group2, ":", nrow(unique_to_g2), "\n")
# Save results with taxonomy
write.csv(shared, paste0("shared_", group1, "_vs_", group2, ".csv"), row.names = FALSE)
write.csv(unique_to_g1, paste0("unique_to_", group1, ".csv"), row.names = FALSE)
write.csv(unique_to_g2, paste0("unique_to_", group2, ".csv"), row.names = FALSE)
}
setwd("~/Desktop/Compare ASVs_16S_New&Old/16S indicator subsets (all ASVs)")
rm(list=ls())
library(VennDiagram)
# List of file paths
files <- list(
"Inoculated_With_P" = "16S-Subset-Group Inoculated withP.csv",
"Inoculated_With_MS" = "16S-Subset-Group Inoculated With MS.csv",
"Uninoculated_With_P" = "16S-Subset-Group Uninoculated withP.csv",
"Uninoculated_With_MS" = "16S-Subset-Group Uninoculated With MS.csv",
"Inoculated_Without_P" = "16S-Subset-Group Inoculated withoutP.csv",
"Inoculated_Without_MS" = "16S-Subset-Group Inoculated Without MS.csv",
"Uninoculated_Without_P" = "16S-Subset-Group Uninoculated withoutP.csv",
"Uninoculated_Without_MS" = "16S-Subset-Group Uninoculated Without MS.csv"
)
View(files)
# Load and clean Family-level taxonomy from each file
family_groups <- lapply(files, function(file) {
data <- read.csv(file, header = TRUE, stringsAsFactors = FALSE)
data <- data[!is.na(data$Family) & data$Family != "", ]  # Filter out NA/blank
unique(data$Family)
})
# Define comparisons
comparisons <- list(
c("Inoculated_With_P", "Inoculated_With_MS"),
c("Uninoculated_With_P", "Uninoculated_With_MS"),
c("Inoculated_Without_P", "Inoculated_Without_MS"),
c("Uninoculated_Without_P", "Uninoculated_Without_MS")
)
View(family_groups)
# Loop through comparisons
for (comparison in comparisons) {
group1 <- comparison[1]
group2 <- comparison[2]
family_g1 <- family_groups[[group1]]
family_g2 <- family_groups[[group2]]
# Compare
shared_families <- intersect(family_g1, family_g2)
unique_to_g1 <- setdiff(family_g1, family_g2)
unique_to_g2 <- setdiff(family_g2, family_g1)
# Console summary
cat("\nComparison:", group1, "vs", group2, "\n")
cat("Shared Families:", length(shared_families), "\n")
cat("Unique to", group1, ":", length(unique_to_g1), "\n")
cat("Unique to", group2, ":", length(unique_to_g2), "\n")
# Save CSVs
write.csv(data.frame(Family = shared_families),
paste0("shared_", group1, "_vs_", group2, ".csv"), row.names = FALSE)
write.csv(data.frame(Family = unique_to_g1),
paste0("unique_to_", group1, ".csv"), row.names = FALSE)
write.csv(data.frame(Family = unique_to_g2),
paste0("unique_to_", group2, ".csv"), row.names = FALSE)
# Make Venn diagram
venn.plot <- draw.pairwise.venn(
area1 = length(family_g1),
area2 = length(family_g2),
cross.area = length(shared_families),
category = c(group1, group2),
fill = c("blue", "red"),
alpha = 0.5,
cat.pos = c(0, 180),
cat.dist = 0.05
)
# Save as PDF
pdf(paste0(group1, "_vs_", group2, "_venn.pdf"))
grid.draw(venn.plot)
dev.off()
}
rm(list=ls())
library(VennDiagram)
# List of file paths
files <- list(
"Inoculated_With_P" = "16S-Subset-Group Inoculated withP.csv",
"Inoculated_With_MS" = "16S-Subset-Group Inoculated With MS.csv",
"Uninoculated_With_P" = "16S-Subset-Group Uninoculated withP.csv",
"Uninoculated_With_MS" = "16S-Subset-Group Uninoculated With MS.csv",
"Inoculated_Without_P" = "16S-Subset-Group Inoculated withoutP.csv",
"Inoculated_Without_MS" = "16S-Subset-Group Inoculated Without MS.csv",
"Uninoculated_Without_P" = "16S-Subset-Group Uninoculated withoutP.csv",
"Uninoculated_Without_MS" = "16S-Subset-Group Uninoculated Without MS.csv"
)
# Load and clean Family-level taxonomy from each file
family_groups <- lapply(files, function(file) {
data <- read.csv(file, header = TRUE, stringsAsFactors = FALSE)
data <- data[!is.na(data$Family) & data$Family != "", ]  # Filter out NA/blank
unique(data$Family)
})
# Define comparisons
comparisons <- list(
c("Inoculated_With_P", "Inoculated_With_MS"),
c("Uninoculated_With_P", "Uninoculated_With_MS"),
c("Inoculated_Without_P", "Inoculated_Without_MS"),
c("Uninoculated_Without_P", "Uninoculated_Without_MS")
)
# Loop through comparisons
for (comparison in comparisons) {
group1 <- comparison[1]
group2 <- comparison[2]
family_g1 <- family_groups[[group1]]
family_g2 <- family_groups[[group2]]
# Compare
shared_families <- intersect(family_g1, family_g2)
unique_to_g1 <- setdiff(family_g1, family_g2)
unique_to_g2 <- setdiff(family_g2, family_g1)
# Console summary
cat("\nComparison:", group1, "vs", group2, "\n")
cat("Shared Families:", length(shared_families), "\n")
cat("Unique to", group1, ":", length(unique_to_g1), "\n")
cat("Unique to", group2, ":", length(unique_to_g2), "\n")
# Save CSVs
write.csv(data.frame(Family = shared_families),
paste0("shared_", group1, "_vs_", group2, ".csv"), row.names = FALSE)
write.csv(data.frame(Family = unique_to_g1),
paste0("unique_to_", group1, ".csv"), row.names = FALSE)
write.csv(data.frame(Family = unique_to_g2),
paste0("unique_to_", group2, ".csv"), row.names = FALSE)
# Make Venn diagram
venn.plot <- draw.pairwise.venn(
area1 = length(family_g1),
area2 = length(family_g2),
cross.area = length(shared_families),
category = c(group1, group2),
fill = c("blue", "red"),
alpha = 0.5,
cat.pos = c(0, 180),
cat.dist = 0.05
)
# Save as PDF
pdf(paste0(group1, "_vs_", group2, "_venn.pdf"))
grid.draw(venn.plot)
dev.off()
}
rm(list=ls())
library(VennDiagram)
# List of file paths
files <- list(
"Inoculated_With_P" = "16S-Subset-Group Inoculated withP.csv",
"Inoculated_With_MS" = "16S-Subset-Group Inoculated With MS.csv",
"Uninoculated_With_P" = "16S-Subset-Group Uninoculated withP.csv",
"Uninoculated_With_MS" = "16S-Subset-Group Uninoculated With MS.csv",
"Inoculated_Without_P" = "16S-Subset-Group Inoculated withoutP.csv",
"Inoculated_Without_MS" = "16S-Subset-Group Inoculated Without MS.csv",
"Uninoculated_Without_P" = "16S-Subset-Group Uninoculated withoutP.csv",
"Uninoculated_Without_MS" = "16S-Subset-Group Uninoculated Without MS.csv"
)
# Load and clean Family-level taxonomy from each file
family_groups <- lapply(files, function(file) {
data <- read.csv(file, header = TRUE, stringsAsFactors = FALSE)
data <- data[!is.na(data$Family) & data$Family != "", ]  # Filter out NA/blank
unique(data$Family)
})
# Define comparisons
comparisons <- list(
c("Inoculated_With_P", "Inoculated_With_MS"),
c("Uninoculated_With_P", "Uninoculated_With_MS"),
c("Inoculated_Without_P", "Inoculated_Without_MS"),
c("Uninoculated_Without_P", "Uninoculated_Without_MS")
)
# Loop through comparisons
for (comparison in comparisons) {
group1 <- comparison[1]
group2 <- comparison[2]
family_g1 <- family_groups[[group1]]
family_g2 <- family_groups[[group2]]
# Compare
shared_families <- intersect(family_g1, family_g2)
unique_to_g1 <- setdiff(family_g1, family_g2)
unique_to_g2 <- setdiff(family_g2, family_g1)
# Console summary
cat("\nComparison:", group1, "vs", group2, "\n")
cat("Shared Families:", length(shared_families), "\n")
cat("Unique to", group1, ":", length(unique_to_g1), "\n")
cat("Unique to", group2, ":", length(unique_to_g2), "\n")
# Save CSVs
write.csv(data.frame(Family = shared_families),
paste0("shared_", group1, "_vs_", group2, ".csv"), row.names = FALSE)
write.csv(data.frame(Family = unique_to_g1),
paste0("unique_to_", group1, ".csv"), row.names = FALSE)
write.csv(data.frame(Family = unique_to_g2),
paste0("unique_to_", group2, ".csv"), row.names = FALSE)
# Make Venn diagram
venn.plot <- draw.pairwise.venn(
area1 = length(family_g1),
area2 = length(family_g2),
cross.area = length(shared_families),
category = c(group1, group2),
fill = c("blue", "red"),
alpha = 0.5,
cat.pos = c(0, 180),
cat.dist = 0.05
)
# Save as PDF
pdf(paste0(group1, "_vs_", group2, "_venn.pdf"))
grid.draw(venn.plot)
dev.off()
}
rm(list=ls())
rm(list=ls())
library(VennDiagram)
rm(list=ls())
library(VennDiagram)
# List of file paths
files <- list(
"Inoculated_With_P" = "16S-Subset-Group Inoculated withP.csv",
"Inoculated_With_MS" = "16S-Subset-Group Inoculated With MS.csv",
"Uninoculated_With_P" = "16S-Subset-Group Uninoculated withP.csv",
"Uninoculated_With_MS" = "16S-Subset-Group Uninoculated With MS.csv",
"Inoculated_Without_P" = "16S-Subset-Group Inoculated withoutP.csv",
"Inoculated_Without_MS" = "16S-Subset-Group Inoculated Without MS.csv",
"Uninoculated_Without_P" = "16S-Subset-Group Uninoculated withoutP.csv",
"Uninoculated_Without_MS" = "16S-Subset-Group Uninoculated Without MS.csv"
)
# Load and clean Family-level taxonomy from each file
family_groups <- lapply(files, function(file) {
data <- read.csv(file, header = TRUE, stringsAsFactors = FALSE)
data <- data[!is.na(data$Family) & data$Family != "", ]  # Filter out NA/blank
unique(data$Family)
})
# Define comparisons
comparisons <- list(
c("Inoculated_With_P", "Inoculated_With_MS"),
c("Uninoculated_With_P", "Uninoculated_With_MS"),
c("Inoculated_Without_P", "Inoculated_Without_MS"),
c("Uninoculated_Without_P", "Uninoculated_Without_MS")
)
View(comparisons)
rm(list=ls())
rm(list=ls())
library(VennDiagram)
setwd("~/Desktop/Compare ASVs_16S_New&Old/16S indicator subsets (all ASVs)")
# List of file paths
files <- list(
"Inoculated_With_P" = "16S-Subset-Group Inoculated withP.csv",
"Inoculated_With_MS" = "16S-Subset-Group Inoculated With MS.csv",
"Uninoculated_With_P" = "16S-Subset-Group Uninoculated withP.csv",
"Uninoculated_With_MS" = "16S-Subset-Group Uninoculated With MS.csv",
"Inoculated_Without_P" = "16S-Subset-Group Inoculated withoutP.csv",
"Inoculated_Without_MS" = "16S-Subset-Group Inoculated Without MS.csv",
"Uninoculated_Without_P" = "16S-Subset-Group Uninoculated withoutP.csv",
"Uninoculated_Without_MS" = "16S-Subset-Group Uninoculated Without MS.csv"
)
# Load and clean Family-level taxonomy from each file
family_groups <- lapply(files, function(file) {
data <- read.csv(file, header = TRUE, stringsAsFactors = FALSE)
data <- data[!is.na(data$Family) & data$Family != "", ]  # Filter out NA/blank
unique(data$Family)
})
View(family_groups)
# Define comparisons
comparisons <- list(
c("Inoculated_With_P", "Inoculated_With_MS"),
c("Uninoculated_With_P", "Uninoculated_With_MS"),
c("Inoculated_Without_P", "Inoculated_Without_MS"),
c("Uninoculated_Without_P", "Uninoculated_Without_MS")
)
# Loop through comparisons
for (comparison in comparisons) {
group1 <- comparison[1]
group2 <- comparison[2]
family_g1 <- family_groups[[group1]]
family_g2 <- family_groups[[group2]]
# Compare
shared_families <- intersect(family_g1, family_g2)
unique_to_g1 <- setdiff(family_g1, family_g2)
unique_to_g2 <- setdiff(family_g2, family_g1)
# Console summary
cat("\nComparison:", group1, "vs", group2, "\n")
cat("Shared Families:", length(shared_families), "\n")
cat("Unique to", group1, ":", length(unique_to_g1), "\n")
cat("Unique to", group2, ":", length(unique_to_g2), "\n")
# Save CSVs
write.csv(data.frame(Family = shared_families),
paste0("shared_", group1, "_vs_", group2, ".csv"), row.names = FALSE)
write.csv(data.frame(Family = unique_to_g1),
paste0("unique_to_", group1, ".csv"), row.names = FALSE)
write.csv(data.frame(Family = unique_to_g2),
paste0("unique_to_", group2, ".csv"), row.names = FALSE)
# Make Venn diagram
venn.plot <- draw.pairwise.venn(
area1 = length(family_g1),
area2 = length(family_g2),
cross.area = length(shared_families),
category = c(group1, group2),
fill = c("blue", "red"),
alpha = 0.5,
cat.pos = c(0, 180),
cat.dist = 0.05
)
# Save as PDF
pdf(paste0(group1, "_vs_", group2, "_venn.pdf"))
grid.draw(venn.plot)
dev.off()
}
rm(list=ls())
